package com.hollycrm.hollysqm.core.util;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;

import com.hollycrm.hollysqm.core.target.DocColumn;

/**
 * 将doc对象转换为实体类对象
 * @author admin
 *
 */
public class DocToBeanUtil {

	/**
	 * 将文档对象转换为实体类
	 * @param record
	 * @param clazz
	 * @return
	 */
	public static <T>T docToBean(SolrDocument record, Class<T> clazz) {
		T o = null;
		try {
			o = clazz.newInstance();
		} catch (InstantiationException e1) {
			e1.printStackTrace();
		} catch (IllegalAccessException e1) {
			e1.printStackTrace();
		}
		Field[] fields = clazz.getDeclaredFields();
		for (Field field : fields) {
			Object value = record.get(field.getName());
			try {
				BeanUtils.setProperty(o, field.getName(), value);
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
		}
		return o;
	}
	
	/**
	 * 将文档对象转换为实体类,只获取带有@DocColumn注解的字段值
	 * @param <T>
	 * @param record
	 * @param clazz
	 * @return
	 */
	public static <T> T docToAnnBean(SolrDocument record, Class<T> clazz) {
		T o = null;
		try {
			o = clazz.newInstance();
		} catch (InstantiationException e1) {
			e1.printStackTrace();
		} catch (IllegalAccessException e1) {
			e1.printStackTrace();
		}
		Object value = null;
		String columnName = null;
		Field[] fields = clazz.getDeclaredFields();
		for (Field field : fields) {
			if (field.isAnnotationPresent(DocColumn.class)) {//在对象字段上加了注解才有效匹配
				columnName = field.getAnnotation(DocColumn.class).name();//取出定义的名称
			} else {
				continue;
			}
			if (columnName==null || columnName.trim().length()==0){
				columnName = field.getName();
			}
			value = record.get(columnName);
			try {
				BeanUtils.setProperty(o, field.getName(), value);
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
		}
		return o;
	}
	
	/**
	 * 获取批量doc转换实体类的集合
	 * @param records
	 * @param clazz
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static <T> List<T> docToBeanList(SolrDocumentList records, Class<T> clazz) {
		List<T> list = new ArrayList();
		for (SolrDocument record : records) {
			list.add(docToBean(record, clazz));
		}
		return list;
	}

	/**
	 * 将实体对象转换为文档类
	 * @param record
	 * @param clazz
	 * @return
	 */
	public static SolrInputDocument beanToDoc(Object o) {
		SolrInputDocument doc=new SolrInputDocument();
		Field[] fields = o.getClass().getDeclaredFields();//取出所有字段
		for (Field field : fields) {
			String name = field.getName();
			try {
		        doc.setField(name,BeanUtils.getProperty(o, field.getName()));//获取字段值
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			} catch (NoSuchMethodException e){
				e.printStackTrace();
			}
		}
		return doc;
	}
	
	
	/**
	 * 将实体对象转换为文档类,只获取带有@DocColumn注解的字段值
	 * @param record
	 * @param clazz
	 * @return
	 */
	public static SolrInputDocument beanToAnnDoc(Object o) {
		String columnName = null;
		SolrInputDocument doc=new SolrInputDocument();
		Field[] fields = o.getClass().getDeclaredFields();//取出所有字段
		for (Field field : fields) {
			if (field.isAnnotationPresent(DocColumn.class)) {//在对象字段上加了注解才有效匹配
				columnName = field.getAnnotation(DocColumn.class).name();//取出定义的名称
			} else {
				continue;
			}
			if (columnName==null || columnName.trim().length()==0){
				columnName = field.getName();
			}
			try {
		        doc.setField(columnName,BeanUtils.getProperty(o, field.getName()));//获取字段值
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			} catch (NoSuchMethodException e){
				e.printStackTrace();
			}
		}
		return doc;
	}
	
	/**
	 * 获取批量实体类转换doc的集合
	 * @param records
	 * @param clazz
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public static List<SolrInputDocument> beanToDocList(List beanList) {
		List<SolrInputDocument> list = new ArrayList<>();
		for (Object obj : beanList) {
			list.add(beanToDoc(obj));
		}
		return list;
	}
}
